import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../../services/ticket.service';
import { AddCostDetailComponent } from '../add-cost-detail/add-cost-detail.component';

@Component({
  selector: 'app-edit-collection-invoice',
  templateUrl: './edit-collection-invoice.component.html',
  styleUrls: ['./edit-collection-invoice.component.less', '../../../../commom/less/box.less', '../../../../commom/less/expend-but.less']
})
export class EditCollectionInvoiceComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('infoSf', { static: false })
  infoSf!: SFComponent;
  infoSchema: SFSchema = this.initInfoSF();
  @ViewChild('searchSf', { static: false })
  searchSf!: SFComponent;
  searchSchema: SFSchema = this.initSearchSF();
  columns: STColumn[] = this.initST();

  selectedRows: any[] = [];
  _$expand = false;

  constructor(public service: TicketService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.searchSf) {
      Object.assign(requestOptions.body, { ...this.searchSf.value });
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addCostDetailAction() {
    const modal = this.nzModalService.create({
      nzTitle: '添加核销明细',
      nzContent: AddCostDetailComponent,
      nzWidth: 850,
      nzOkText: '保存',
      nzOnOk: com => {
        return false;
      }
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.st.load();
      }
    });
  }

  goBack() {
    history.go(-1);
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.searchSf?.setValue('/expand', this._$expand);
  }

  private initInfoSF(): SFSchema {
    return {
      properties: {
        orderSn: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'text'
          },
          default: '天津怡亚通物流科技有限公司'
        },
        orde1rSn: {
          type: 'string',
          title: '销售方',
          ui: {
            widget: 'text'
          },
          default: '天津怡亚通物流科技有限公司'
        },
        ord0erSn1: {
          type: 'string',
          title: '收票类型',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        orderSn2: {
          title: '发票日期',
          type: 'string',
          ui: {
            widget: 'date',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        orderS3: {
          type: 'string',
          title: '发票号',
          ui: {
            placeholder: '请输入'
          }
        },
        orderSn4: {
          type: 'string',
          title: '收票备注',
          ui: {
            placeholder: '请输入'
          }
        }
      },
      required: ['']
    };
  }

  private initSearchSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        orderSn: {
          type: 'string',
          title: '费用单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        orderS2n: {
          type: 'string',
          title: '订单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount(),
          }
        },
        createTime: {
          title: '费用日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        createTi2me: {
          title: '订单日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '序号', index: 'no', type: 'link' },
      { title: '费用号', index: 'no' },
      { title: '费用日期', index: 'no', type: 'date' },
      { title: '订单号', index: 'callNo' },
      { title: '订单日期', index: 'callNo' },
      { title: '结算客户', render: 'call1No' },
      { title: '费用科目', render: 'call1N2o' },
      { title: '费用金额', index: 'callNo' },
      { title: '收票金额', index: 'updatedAt', type: 'date' },
      { title: '收票税额', index: 'updatedAt' }
    ];
  }
}
